package leader.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import beanDao.MapToBean;

import teacher.bean.Teacher;
import tools.StringDate;
import database.dao.MysqlCrud;

public class DirectTeacherService {
	public Teacher selectTeacherById(Teacher lea) {
		Teacher teacher = new Teacher();
	//	String sql="select * from Teacher where direction=?";
		String sql="select * from teacher where teaId = ?";
		String[] parameter={String.valueOf(lea.getTeaId())};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list= mysqlcurd.SelectData(sql,parameter);
		if(list.size()>0) {
			Map map=list.get(0);
			try {
				teacher.setTeaId(Integer.parseInt(map.get(0).toString()));
				teacher.setName((String) map.get(1));
				teacher.setUser((String) map.get(2));
				teacher.setPassword((String) map.get(3));
				teacher.setSubject(Integer.parseInt(map.get(4).toString()));
				teacher.setDirection(Integer.parseInt(map.get(5).toString()));
				teacher.setDuty(map.get(6).toString());
				teacher.setDegree(Integer.parseInt(map.get(7).toString()));
				teacher.setOnline(Integer.parseInt(map.get(8).toString()));
				teacher.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(map.get(9).toString()));
				teacher.setFurStud(map.get(10).toString());
				teacher.setTopSchool(map.get(11).toString());
				teacher.setTopMajor(map.get(12).toString());
				teacher.setMail(map.get(13).toString());
				teacher.setPower(Integer.parseInt(map.get(14).toString()));
				teacher.setInSubDate(new SimpleDateFormat("yyyy-MM-dd").parse(map.get(15).toString()));

			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}		
			return teacher;
		} else {
			return null;
		}
	}
	
	public boolean updateTeacherInfo(Teacher teacherOne,Teacher teacherTwo) {
		boolean flag = false;
		String sql="update teacher set duty = ?, degree = ?, online = ?," +
				"birthday = ?, furStud = ?, topSchool = ?, topMajor = ?, mail = ?, inSubDate=? where teaId = ?";
		String[] parameter={
				String.valueOf(teacherOne.getDuty()),
				String.valueOf(teacherOne.getDegree()),
				String.valueOf(teacherOne.getOnline()),
				String.valueOf(teacherOne.getBirthday()),
				String.valueOf(teacherOne.getFurStud()),
				String.valueOf(teacherOne.getTopSchool()),
				String.valueOf(teacherOne.getTopMajor()),
				String.valueOf(teacherOne.getMail()),
				String.valueOf(teacherOne.getInSubDate()),
				String.valueOf(teacherTwo.getTeaId())
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		flag = mysqlcurd.InsertAndUpdateData(sql, parameter);
		return flag;
	}
	
	public int getCountOfTeacherByUser(String user){
		int sum=0;
		String sql="select count(*) from teacher where user=?";
		String[] parameter = {
				String.valueOf(user)
		};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists = mysqlcurd.SelectData(sql, parameter);
		Map map = lists.get(0);
		sum = Integer.parseInt(map.get(0).toString());
		return sum;
	}
	
	public int SelectTeaIdByUser(String user){
		int TeaId;
		String sql="select teaId from teacher where user=?";
		String[] parameter = {
				String.valueOf(user),
		};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists = mysqlcurd.SelectData(sql, parameter);
		Map map = lists.get(0);
		TeaId = Integer.parseInt(map.get(0).toString());
		return TeaId;
	}
	
	public List<Teacher> selectSegregateAllTeacher(Teacher tea,int pageNow,int pageSize) {
		List<Teacher> teach = new ArrayList<Teacher>();
		String sql="select * from (select * from Teacher order by direction,degree) as T where T.direction=? and T.teaId LIMIT " +
				""+(pageNow-1)+" ,"+pageSize+"";
		String[] parameter={String.valueOf(tea.getDirection())};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list= mysqlcurd.SelectData(sql, parameter);
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			teach.add(new MapToBean().MapToTeacher(map));
		}
		return teach;
	}
	
	public int getCountOfTeacher(Teacher teacher) {
		int sum=0;
		String sql="select count(*) from teacher where direction=?";
		String[] parameter={String.valueOf(teacher.getDirection())};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql,parameter);
		Map map=lists.get(0);
		sum=Integer.parseInt(map.get(0).toString());
		return sum;
	}
}
